package org.edu.modules.common.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class PhoneUtil {
    // 验证手机号的正则表达式
    private static final String MOBILE_PATTERN = "^(13[0-9]|14[5-9]|15[0-3,5-9]|16[2,5-7]|17[0-9]|18[0-9]|19[1,3,5,8,9])\\d{8}$";
    // 验证座机号的正则表达式（带区号或不带区号，允许空格、短横线或没有分隔符）
    private static final String PHONE_PATTERN = "^(?:0\\d{2,4}[-\\s]?)?\\d{7,8}$";
    // 编译正则表达式为Pattern对象，以提高性能
    private static final Pattern MOBILE_PATTERN_COMPILED = Pattern.compile(MOBILE_PATTERN);
    private static final Pattern PHONE_PATTERN_COMPILED = Pattern.compile(PHONE_PATTERN);

    public static boolean isMobile(String mobile) {
        String regex = "(\\+\\d+)?1[3458]\\d{9}$";
        return Pattern.matches(regex, mobile);
    }

    public static boolean isPostCode(String postCode) {
        String reg = "[1-9]\\d{5}";
        return Pattern.matches(reg, postCode);
    }

    //座机和手机号一起判断
    public static boolean isMobileOrPhone(String mobile) {
        Matcher mobileMatcher = MOBILE_PATTERN_COMPILED.matcher(mobile);
        if (mobileMatcher.matches()) {
            return true; // 是有效的手机号
        }
        Matcher phoneMatcher = PHONE_PATTERN_COMPILED.matcher(mobile);
        return phoneMatcher.matches(); // 是有效的座机号或仍然不匹配（返回false）
    }
}
